﻿
Imports System.Data

Imports System
Imports iTextSharp.text
Imports iTextSharp.text.pdf
Imports System.IO


Partial Class Content_Reportes_infoCliente
    Inherits System.Web.UI.Page

    Dim IDataBase As New Clases.DataBase
    Dim ECConnString As String = ConfigurationManager.AppSettings("dbIP")
    Dim IControles As New Clases.controles



    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim nombreReporte As String = "InfoCliente.pdf"
        Dim dt As New DataTable

        IDataBase.createDataTable(dt, Session("infoClientes"))

        GenerarPdf(dt, nombreReporte)
        Response.Redirect("../../Files/" + nombreReporte)
    End Sub

    Protected Sub GenerarPdf(ByVal tblReporte As DataTable, ByVal NombreReporte As String)

        'Dimensiones del Docuemnto
        Dim documento As Document = New Document(PageSize.LETTER, 50, 100, 20, 100)
        ' step 2: creamos el documento            
        'Ruta donde se encuentra nuestra aplicacion, en esta ruta genera el Pdf esta ruta es la del servidor
        Dim writerPdf As PdfWriter = PdfWriter.GetInstance(documento, New FileStream(AppDomain.CurrentDomain.BaseDirectory & "Files\" & NombreReporte, FileMode.Create))

        'step 3: abrimos el documento
        documento.Open()

        'Titulo del archivo
        Dim font24B As Font = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 14, 0, BaseColor.WHITE)
        '

        Dim cb As PdfContentByte = writerPdf.DirectContent
        Dim ct As ColumnText = New ColumnText(cb)



        Dim logo As Image = iTextSharp.text.Image.GetInstance(Server.MapPath("../../Images/logo.jpg"))
        logo.SetAbsolutePosition(200, 750)
        logo.ScalePercent(75)
        documento.Add(logo)

        Dim seperator As iTextSharp.text.pdf.draw.VerticalPositionMark = New iTextSharp.text.pdf.draw.LineSeparator
        seperator.Offset = -30

        documento.Add(seperator)

        ct.Go()
        'Creacion de la tabla que se mostrara en el Pdf
        Dim table As PdfPTable = New PdfPTable(2)

        'actual width of table in points
        table.TotalWidth = 500

        'fix the absolute width of the table
        table.LockedWidth = True
        'documento.Add(table)
        table.SplitLate = True
        table.SplitRows = True


        Dim cell As PdfPCell = New PdfPCell(New Phrase("Información Personal del Cliente", font24B))
        cell.Colspan = 2
        cell.HorizontalAlignment = 1
        cell.BackgroundColor = New BaseColor(53, 144, 165)
        table.AddCell(cell)

        Dim LetraTituloTabla As Font = FontFactory.GetFont(FontFactory.HELVETICA_BOLD, 10, Font.BOLDITALIC)

        'Cremos las cabeceras de la tabla
        Dim celdaNombre As PdfPCell = New PdfPCell(New Phrase("Nombre", LetraTituloTabla))
        Dim celdaApellidoP As PdfPCell = New PdfPCell(New Phrase("Apellido Paterno", LetraTituloTabla))
        Dim celdaApellidoM As PdfPCell = New PdfPCell(New Phrase("Apellido Materno", LetraTituloTabla))
        Dim celdaRFC As PdfPCell = New PdfPCell(New Phrase("RFC", LetraTituloTabla))
        Dim celdaCURP As PdfPCell = New PdfPCell(New Phrase("CURP", LetraTituloTabla))
        Dim celdaCalle As PdfPCell = New PdfPCell(New Phrase("Calle", LetraTituloTabla))
        Dim celdaNumero As PdfPCell = New PdfPCell(New Phrase("Numero", LetraTituloTabla))
        Dim celdaColonia As PdfPCell = New PdfPCell(New Phrase("Colonia", LetraTituloTabla))
        Dim celdaPais As PdfPCell = New PdfPCell(New Phrase("Pais", LetraTituloTabla))
        Dim celdaEstado As PdfPCell = New PdfPCell(New Phrase("Estado", LetraTituloTabla))
        Dim celdaCiudad As PdfPCell = New PdfPCell(New Phrase("Municipio", LetraTituloTabla))
        Dim celdaCP As PdfPCell = New PdfPCell(New Phrase("Código Postal", LetraTituloTabla))
        Dim celdaCorreo As PdfPCell = New PdfPCell(New Phrase("Correo Electrónico", LetraTituloTabla))

        ' recorremos todos las filas del Datatable
        For Each fila As DataRow In tblReporte.Rows
            table.AddCell(celdaNombre)
            table.AddCell(fila(1).ToString())

            table.AddCell(celdaApellidoP)
            table.AddCell(fila(2).ToString())

            table.AddCell(celdaApellidoM)
            table.AddCell(fila(3).ToString())

            table.AddCell(celdaRFC)
            table.AddCell(fila(4).ToString())

            table.AddCell(celdaCURP)
            table.AddCell(fila(5).ToString())

            table.AddCell(celdaCalle)
            table.AddCell(fila(6).ToString())

            table.AddCell(celdaNumero)
            table.AddCell(fila(7).ToString())

            table.AddCell(celdaColonia)
            table.AddCell(fila(8).ToString())

            table.AddCell(celdaPais)
            table.AddCell(fila(9).ToString())

            table.AddCell(celdaEstado)
            table.AddCell(fila(10).ToString())

            table.AddCell(celdaCiudad)
            table.AddCell(fila(11).ToString())

            table.AddCell(celdaCP)
            table.AddCell(fila(12).ToString())

            table.AddCell(celdaCorreo)
            table.AddCell(fila(13).ToString())
        Next


        table.WriteSelectedRows(0, -1, 50, 720, writerPdf.DirectContent)

        documento.Close()

    End Sub

End Class
